package com.project.class4_8.proxy2.dao.impl;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;

public class BaseDao {
    protected Connection con;
    protected PreparedStatement ps;
    protected ResultSet rs;

    /**
     * 创建连接
     *
     * @return 返回连接对象
     */
    public static Connection getConnection() {
        try {
            //加载驱动
            Class.forName("com.mysql.jdbc.Driver");
            //建立连接
            Connection c = DriverManager.getConnection(
                    "jdbc:mysql://localhost:12345/product?characterEncoding=utf-8",
                    "root","lovo");
            return c;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 建立连接
     *
     * @param con 连接对象
     */
    public void setConnection(Connection con) {
        this.con = con;
    }

    public void updateData(String sql,Object... valueArray){
        try {
            ps = con.prepareStatement(sql);
            //设置占位符
            for (int i = 0 ; i < valueArray.length ;i++){
                ps.setObject(i+1,valueArray[i]);
            }

            ps.executeUpdate();

        }catch (Exception e){
            e.printStackTrace();
        }
    }
}
